﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IService.Business;
using IService.Model;
using IService.DataDLL.Utility;

namespace IService.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {

            }
        }

        protected void BtnSubmit_Click(object sender, EventArgs e)
        {
            bool flag = false;
            DTVAdmin admin = null;
            string username = this.TxtUsername.Text;
            string password = this.TxtPassword.Text;
            if (!LoginSuperAdmin(username, password))
            {
                //如果不是超级管理员，则使用MD5加密密码
                password = Md5Helper.md5(password, 32);
                admin = DTVBusiness.AdminManager.LoginByParam(username, password);
                if (admin == null)
                {
                    this.LblReslutMessage.Text = "<font color='red'>用户名或密码错误！</font>";
                }
                else
                {
                    flag = true;
                    Session.Add("AdminSession", admin);
                }
            }
            else
            {
                flag = true;
                admin = new DTVAdmin();
                admin.Username = username;
                admin.DisplayName = "超级管理员";
                Session.Add("AdminSession", admin);
            }
            if (flag)
            {
                Session.Timeout = 1000;
                Response.Redirect("~/Admin/Index.aspx");
            }
        }
        /// <summary>
        /// 以超级管理员登录，配置文件中
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        protected bool LoginSuperAdmin(string username, string password)
        {
            string usernameconfig = StringUtil.GetAppConfigString("SuperUsername");
            string passwordconfig = StringUtil.GetAppConfigString("SuperPassword");
            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                return false;
            }
            else
            {
                if (username == usernameconfig && password == passwordconfig)
                {
                    return true;
                }
            }
            return false;
        }
    }
}